<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('quick Search Placeholder', { fields: t('xPotcheck.quick Search Fields') })"
        >
        <el-button v-blur class="table-header-operate" 
        type="primary" 
        :disabled="baTable.table.data?.length==0" 
        style="margin-left: 12px"
        @click="handleExport">
            <span class="table-header-operate-text" >导出数据</span>
        </el-button>
        </TableHeader>
        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" />

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted } from 'vue'
import baTableClass from '/@/utils/baTable'
import { xPotcheck } from '/@/api/controllerUrls'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import fileDownload from 'js-file-download'
import createAxios from '/@/utils/axios'

const { t } = useI18n()
const tableRef = ref()
const optButtons = defaultOptButtons(["edit","delete"])
const baTable = new baTableClass(
    new baTableApi(xPotcheck),
    {
        pk: 'NJNO',
        column: [
            { type: 'selection', align: 'center', operator: false },
            // { label: t('xPotcheck.NJNO'), prop: 'NJNO', align: 'center' },
            { label: t('xPotcheck.TCNO'), prop: 'TCNO', align: 'center' ,operator:false},
            { label: t('xPotcheck.CHECKDATE'), prop: 'CHECKDATE', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 ,
            timeFormat:'yy-mm-dd'},
            { label: t('xPotcheck.CHECKTYPE'), prop: 'CHECKTYPE', align: 'center',operator:false },
            { label: t('xPotcheck.NEXTCHECKDATE'), prop: 'NEXTCHECKDATE', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 ,
            timeFormat:'yy-mm-dd'
        },
            { label: t('xPotcheck.NEXTCHECKTYPE'), prop: 'NEXTCHECKTYPE', align: 'center',operator:false },
            { label: t('xPotcheck.PASSED'), prop: 'PASSED', align: 'center',show:false,render: 'tag',replaceValue: { '0': '否', '1': '是' }, },
            { label: t('xPotcheck.OP'), prop: 'OP', align: 'center' ,show:false,operator:false},
            { label: t('xPotcheck.OPTIME'), prop: 'OPTIME', align: 'center', render: 'datetime', sortable: 'custom', width: 160,show:false,operator:false },
            { label: t('xPotcheck.UPLOADER'), prop: 'UPLOADER', align: 'center',show:false,operator:false },
            { label: t('xPotcheck.UPLOADTIME'), prop: 'UPLOADTIME', align: 'center', render: 'datetime', sortable: 'custom',  width: 160,show:false,operator:false },
            { label: t('xPotcheck.FINISHFILE'), prop: 'FINISHFILE', align: 'center', render: 'url', sortable: 'custom',  width: 160,operator:false },
            
            { label: t('operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: [undefined, ],
        defaultOrder: { prop: 'NJNO', order: 'desc' },
    },
    {
        defaultItems: [],
    }
)

provide('baTable', baTable)

const route = useRoute()
onMounted(() => {
    baTable.table.ref = tableRef.value
    if(route.query.code){
        baTable.table.filter.code = route.query.code
    }
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})

const handleExport = (item:any)=>{
    createAxios({
        url:"/admin/XPotcheck/out",
        method: 'get',
        params: baTable.table.filter,
        responseType:'blob'
    },
    { reductDataFormat: false }
    ).then((response:any)=>{
        fileDownload(response.data, '年检记录.xlsx')
    })
}

</script>

<script lang="ts">
import { defineComponent } from 'vue'
import { useRoute } from 'vue-router'
export default defineComponent({
    name: 'xPotcheck',
})
</script>

<style scoped lang="scss"></style>
